package com.liao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.common.Result;
import com.liao.dto.BorrowDTO;
import com.liao.dto.ReturnDTO;
import com.liao.entity.BorrowRecord;
import com.liao.service.BorrowRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 借阅记录控制器
 */
@RestController
@RequestMapping("/borrows")
public class BorrowRecordController {

    @Resource
    private BorrowRecordService borrowRecordService;

    /**
     * 获取借阅记录分页列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param userId  用户ID（可选）
     * @param bookId  图书ID（可选）
     * @param status  状态（可选）
     * @return 分页借阅记录列表
     */
    @GetMapping("/page")
    public Result<Page<BorrowRecord>> getBorrowPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long bookId,
            @RequestParam(required = false) Integer status) {
        Page<BorrowRecord> page = new Page<>(current, size);
        Page<BorrowRecord> borrowPage = borrowRecordService.getBorrowPage(page, userId, bookId, status);
        return Result.success(borrowPage);
    }

    /**
     * 获取借阅记录详情
     *
     * @param id 借阅记录ID
     * @return 借阅记录详情
     */
    @GetMapping("/{id}")
    public Result<BorrowRecord> getBorrowDetail(@PathVariable Long id) {
        BorrowRecord borrowRecord = borrowRecordService.getById(id);
        if (borrowRecord == null) {
            return Result.error("借阅记录不存在");
        }
        return Result.success(borrowRecord);
    }

    /**
     * 借阅图书
     *
     * @param borrowDTO 借阅信息
     * @return 借阅结果
     */
    @PostMapping("/borrow")
    public Result<BorrowRecord> borrowBook(@RequestBody @Valid BorrowDTO borrowDTO) {
        BorrowRecord borrowRecord = borrowRecordService.borrowBook(borrowDTO);
        return Result.success("借阅成功", borrowRecord);
    }

    /**
     * 归还图书
     *
     * @param returnDTO 归还信息
     * @return 归还结果
     */
    @PostMapping("/return")
    public Result<BorrowRecord> returnBook(@RequestBody @Valid ReturnDTO returnDTO) {
        BorrowRecord borrowRecord = borrowRecordService.returnBook(returnDTO);
        return Result.success("归还成功", borrowRecord);
    }

    /**
     * 获取用户的借阅历史
     *
     * @param userId  用户ID
     * @param current 当前页
     * @param size    每页大小
     * @return 用户借阅历史
     */
    @GetMapping("/user/{userId}")
    public Result<Page<BorrowRecord>> getUserBorrowHistory(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<BorrowRecord> page = new Page<>(current, size);
        Page<BorrowRecord> borrowPage = borrowRecordService.getUserBorrowHistory(page, userId);
        return Result.success(borrowPage);
    }

    /**
     * 获取图书的借阅历史
     *
     * @param bookId  图书ID
     * @param current 当前页
     * @param size    每页大小
     * @return 图书借阅历史
     */
    @GetMapping("/book/{bookId}")
    public Result<Page<BorrowRecord>> getBookBorrowHistory(
            @PathVariable Long bookId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<BorrowRecord> page = new Page<>(current, size);
        Page<BorrowRecord> borrowPage = borrowRecordService.getBookBorrowHistory(page, bookId);
        return Result.success(borrowPage);
    }
}